package com.xiyue.lenovo.handlertest;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    //定义周期性显示的图片
    int[] imageIds = new int[]{
        R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,
        R.drawable.a6,R.drawable.a7,R.drawable.a8,R.drawable.a9,R.drawable.a10,
        R.drawable.a11,R.drawable.a12,R.drawable.a13,R.drawable.a14,R.drawable.a15,
        R.drawable.a16,R.drawable.a17,R.drawable.a18,R.drawable.a19,R.drawable.a20,
        R.drawable.a21,R.drawable.a22,R.drawable.a23,R.drawable.a24,R.drawable.a25,
        R.drawable.a26,R.drawable.a27,R.drawable.a28,R.drawable.a29,R.drawable.a30,
        R.drawable.a31,R.drawable.a32,R.drawable.a33,R.drawable.a34,R.drawable.a35,
        R.drawable.a36,R.drawable.a37,R.drawable.a38,R.drawable.a39,R.drawable.a40,
        R.drawable.a41,R.drawable.a42,R.drawable.a43,R.drawable.a44,R.drawable.a45,
        R.drawable.a46,R.drawable.a47,R.drawable.a48,R.drawable.a49,R.drawable.a50,
        R.drawable.a51,R.drawable.a52,R.drawable.a53,R.drawable.a54,R.drawable.a55,
        R.drawable.a56,R.drawable.a57,R.drawable.a58,R.drawable.a59,R.drawable.a60,
        R.drawable.a61,R.drawable.a62,R.drawable.a63,R.drawable.a65,R.drawable.a65
    };
    int currentImageId=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView show = (ImageView) findViewById(R.id.show);
        final Handler myHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                //如果该消息是本程序所发送的
                if(msg.what==0X1233){
                    //动态的修改所显示的图片
                    show.setImageResource(imageIds[currentImageId++]);
                }
                super.handleMessage(msg);
            }
        };
        //定义一个计时器，让该计时器周期性地执行指定的任务
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                //发送空消息
                myHandler.sendEmptyMessage(0x1233);
            }
        },0,1200);
    }
}
